<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 * @author yxx
 * @param 工作任务
 * @date 2017-10-12
 */
class Work extends MY_Controller {

	    public function __construct() {
	        parent::__construct();
	        $this->load->model('M_work');
	        $this->load->model('M_worktask');
	    }




	/**下发工作任务
	 *@param $PID
	*/
	public function work_task(){
		$userid = $this->input->get_post("userid", true);//用户ID
		$title = $this->input->get_post("worktitle", true);//任务主题
		$content = $this->input->get_post("workcontent", true);//任务内容
		$pubid = $this->input->get_post("getuserid", true);//执行人ID
		$starttime = $this->input->get_post("workstarttime", true);//任务开始时间
		$endtime = $this->input->get_post("workendtime", true);//任务截止时间
		$type = $this->input->get_post("worktype", true);//提醒类型
		$file = $this->input->get_post("workfile", true);//file 文件
		if(empty($userid)){
		       exit(json_encode(array('code'=>1,'info'=>'下发人ID不能为空')));
		}
		if(empty($title)){
		       exit(json_encode(array('code'=>1,'info'=>'任务主题不能为空')));
		}
		if(empty($content)){
		       exit(json_encode(array('code'=>1,'info'=>'任务内容不能为空')));
		}
		if(empty($pubid)){
		       exit(json_encode(array('code'=>1,'info'=>'执行人id不能为空')));
		}
		if(empty($starttime)){
		       exit(json_encode(array('code'=>1,'info'=>'任务开始时间不能为空')));
		}
		if(empty($endtime)){
		       exit(json_encode(array('code'=>1,'info'=>'任务结束时间不能为空')));
		}
		$sql = "select user_name from t_user where user_id={$pubid}";
		$uname = $this->db->query($sql)->row_array();
		$sql = "select user_name from t_user where user_id={$userid}";
		$uname1 = $this->db->query($sql)->row_array();
		$data = array(
                                       'work_title'=>$title,
                                       'work_content'=>$content,
                                       'work_starttime'=>$starttime,
                                       'work_endtime'=>$endtime,
                                       'work_type'=>$type,
                                       'work_file'=>$file,
                                       'work_addtime'=>time(),
                                       'work_status'=>0,
                                       'get_user_id'=>$pubid,
                                       'get_user_name'=>$uname['user_name'],
                                       'set_user_id'=>$userid,
                                       'set_user_name'=>$uname1['user_name'],

			);
		$insert_id = $this->M_work->saveData($data);
		if($insert_id){
			$data = array(
                                                    'work_task_status'=>0,
                                                    'work_task_time'=>0,
                                                    'addtime'=>time(),
                                                    'work_done_time'=>0,
                                                    'work_result'=>0,
                                                    'work_id'=>$insert_id,
                                                    'user_id'=>$pubid,
				);
			$insert_id1 = $this->M_worktask->saveData($data);
			if($insert_id1){
				//修改任务状态
				$updsql = "UPDATE t_work SET work_status=1 WHERE work_id ='{$insert_id}'";
				$this->db->query($updsql);
			}
			exit(json_encode(array('code'=>0,'info'=>'工作任务下发成功')));
		}else{
			exit(json_encode(array('code'=>1,'info'=>'工作任务下发失败')));
		}
	}
	/**接收工作任务
	 *@param $PID
	*/
	public function get_worktask(){
		$workid = $this->input->get_post("workid", true);//用户ID
		if(empty($workid)){
		       exit(json_encode(array('code'=>1,'info'=>'任务id不能为空')));
		}
		$sql = "select work_status,work_id from t_work where work_id = '{$workid}'";
		$data = $this->db->query($sql)->row_array();
		if(empty($data)){
			exit(json_encode(array('code'=>1,'info'=>'没有此任务')));
		}
		if($data['work_status']==2){
			exit(json_encode(array('code'=>1,'info'=>'任务无法接收请刷新')));
		}else{
			$time = time();
			//改变任务状态
			$updsql = "UPDATE t_work SET work_status=2 WHERE work_id ='{$workid}'";
			$res = $this->db->query($updsql);
			$updsql1 = "UPDATE t_work_task SET work_task_status=1,work_task_time={$time} WHERE work_id ='{$workid}'";
		             $res1 = $this->db->query($updsql1);
		             exit(json_encode(array('code'=>0,'info'=>'任务接收成功')));

		}
	}
	/**完成工作任务
	 *@param $PID
	*/
	public function get_worktask_result(){
		$workid = $this->input->get_post("workid", true);//用户ID
		if(empty($workid)){
		       exit(json_encode(array('code'=>1,'info'=>'任务id不能为空')));
		}
		$time = time();
		$updsql = "UPDATE t_work_task SET work_result=1,work_done_time={$time},work_task_status=1 WHERE work_id ='{$workid}'";
		$res = $this->db->query($updsql);
		if($res){
		             exit(json_encode(array('code'=>0,'info'=>'工作任务完成')));
		}

	}
	/**工作任务详情
	 *@param $PID
	*/
	public function work_details(){
		$workid = $this->input->get_post("workid", true);//任务ID
		if(empty($workid)){
		       exit(json_encode(array('code'=>1,'info'=>'任务id不能为空')));
		}
		$sql = "select k.*,t.work_task_status from t_work k inner join t_work_task t on k.work_id=t.work_id where k.work_id={$workid}";
		$work = $this->db->query($sql)->row_array();
	             $this->printJson_data($work);
		
		
	}
	/**工作任务列表
	 *@param $PID
	*/
	public function work_task_info(){
		$userid = $this->input->get_post("userid", true);//任务ID
		if(empty($userid)){
		       exit(json_encode(array('code'=>1,'info'=>'任务id不能为空')));
		}
		$sql = "select k.*,t.work_task_status,t.work_result from t_work k inner join t_work_task t on t.work_id=k.work_id where k.get_user_id={$userid}";
		$data = $this->M_worktask->querylist($sql);
		$this->printJson_data($data);
	}
	

}